# -*- coding: utf-8 -*-
from five import grok
from zope import schema
from plone.directives import form
from zope.interface import Invalid
from plone.app.textfield import RichText
from plone.namedfile.field import NamedImage
from plone.namedfile.interfaces import IImageScaleTraversable
from z3c.relationfield.schema import RelationList, RelationChoice
from plone.formwidget.contenttree import ObjPathSourceBinder,ContentTreeFieldWidget

from Products.bpContents import _
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from plone.formwidget.autocomplete import AutocompleteFieldWidget
from collective.z3cform.widgets.token_input_widget import TokenInputFieldWidget
from Products.ATVocabularyManager import NamedVocabulary
from collective.gtags.field import Tags

from zope.interface import Interface
from zope.interface import Invalid
from zope import schema
from z3c.form import field, button
from Products.statusmessages.interfaces import IStatusMessage
from Products.CMFCore.interfaces import ISiteRoot
from Products.CMFCore.utils import getToolByName
from Products.bpContents import _
import re

checkEmail = re.compile(r"[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\.)*[a-zA-Z]{2,4}").match
def validateEmail(value):
    if not checkEmail(value):
        raise Invalid(_(u"Invalid email address"))
    return True

MESSAGE_TEMPLATE = """\
Enquiry from: %(name)s <%(emailAddress)s>
%(message)s
"""


class IDepartamento(form.Schema):
    """El tipo de contenido GALERIA, se utiliza tanto para crear un espacio donde poner fotos, como una sala o como 
    """
    cuerpo = RichText(
        title=_(u"Cuerpo"),
        description=_(u"Texto principal"),
    )
    responsables=schema.List(
        title=_(u"Responsables"),
        value_type=schema.Choice(source="bp.StaffMembers",),
    )

    emailAddress = schema.ASCIILine(
        title=_(u"E-mail"),
        description=_(u"Email de contacto para el área"),
        constraint=validateEmail
    )
    telefono = schema.ASCIILine(
        title=_(u"Telefono"),
        description=_(u"Numero de telefono del area ej: 423-6607 int 40 "),
    )
    relaciones = RelationList(
        title=_(u"Documentos Relacionados"),
        description=_(u"Otros articulos o galerias que complementen el documento actual"),
        value_type=RelationChoice(
            source=ObjPathSourceBinder(

            ),
        ),
        required=False,
    )


from plone.app.discussion.interfaces import IConversation
from zope.component import getMultiAdapter
from plone.memoize.instance import memoize
from Products.CMFCore.utils import getToolByName
from plone.directives.dexterity import DisplayForm
from Products.bpContents.BaseClassView import BaseClassView

class View(DisplayForm,BaseClassView):
    grok.context(IDepartamento)
    grok.require('zope2.View')

    def getMembersName(self):
        """Devuelve el nombre"""
        lpersons=[]
        resp=self.context.responsables
        acl_users = getToolByName(self.context, 'acl_users')

        if resp:
            for member_id in resp:
                user = acl_users.getUserById(member_id)
                lpersons.append(user.getProperty('fullname') or member_id)
            if len(lpersons)>0:
                return lpersons
            else:
                return None
        else:
            return None


